/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：实现简单的AES（高级加密标准）加密和解密的功能
* CreateTime：2020-06-22 14:22:11
* Version：1.0
* Modify Recorder：
*************************************************/

namespace TinyToolKit
{
    using System;
    using System.Security.Cryptography;
    using System.Text;

    public class EncryptDecrypt_AES : EncryptDecryptBase 
    {
        private string key = "19950926199509261995092619950926";

        /// <summary>
        /// 32位密钥
        /// </summary>
        public string Key
        {
            set => key = value;
        }

        public override string Encrypt(string content)
        {
            byte[] data = Encoding.UTF8.GetBytes(content);
            byte[] encryptData = Encrypt(data);
            return Convert.ToBase64String(encryptData);
        }

        public override string Decrypt(string content)
        {
            byte[] data = Convert.FromBase64String(content);
            byte[] decryptData = Decrypt(data);
            return Encoding.UTF8.GetString(decryptData);
        }

        public override byte[] Encrypt(byte[] content)
        {
            var aes = new RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(key), 
                Mode = CipherMode.ECB, 
                Padding = PaddingMode.PKCS7
            };

            return aes.CreateEncryptor().TransformFinalBlock(content, 0, content.Length);
        }

        public override byte[] Decrypt(byte[] content) 
        {
            var aes = new RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(key), 
                Mode = CipherMode.ECB, 
                Padding = PaddingMode.PKCS7
            };

            return aes.CreateDecryptor().TransformFinalBlock(content, 0, content.Length);
        }
    }
}
